package cn.java75.aop.interceptor;

import cn.java75.aop.entity.Student;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Arrays;
import java.util.Date;

/**
 * @author oldliu
 * @since 1.0
 */
public class LogInterceptor {
    //环绕拦截
    public Object proceed(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("当前访问的方法是："+joinPoint.getSignature().getName()+",被代理的对象是"+joinPoint.getTarget());
        System.out.println("参数是："+ Arrays.toString(joinPoint.getArgs()));
        if(joinPoint.getSignature().getName().equals("add")){
            Student stu= (Student) joinPoint.getArgs()[0];
            if(stu.getPass()==null||stu.getPass().length()<6){
                throw  new Exception("密码太短");
            }
        }
        Object o=joinPoint.proceed();//放行去执行
        System.out.println("方法调用结束于"+new Date()+"，返回值是"+o);
        return o;
    }

    public Object lanjie(ProceedingJoinPoint joinPoint) throws Throwable {
        long start=System.nanoTime();
        try {
            return joinPoint.proceed();
        } finally {
            long end=System.nanoTime();
            System.out.println("方法:"+joinPoint.getSignature().getName()+"执行花费了："+(end-start)+"纳秒");
        }

    }
}
